/*****************************************************************************

  The following code is derived, directly or indirectly, from the SystemC
  source code Copyright (c) 1996-2006 by all Contributors.
  All Rights reserved.

  The contents of this file are subject to the restrictions and limitations
  set forth in the SystemC Open Source License Version 2.4 (the "License");
  You may not use this file except in compliance with such restrictions and
  limitations. You may obtain instructions on how to receive a copy of the
  License at http://www.systemc.org/. Software distributed by Contributors
  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
  ANY KIND, either express or implied. See the License for the specific
  language governing rights and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  receiver.cpp - This is the implementation file for the asynchronous process
                 "receiver".

  Original Author: Rashmi Goswami, Synopsys, Inc.

 *****************************************************************************/

/*****************************************************************************

  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
  changes you are making here.

      Name, Affiliation, Date:
  Description of Modification:

 *****************************************************************************/

#include "pkt.h"
#include "receiver.h"


void receiver:: entry()
{
    pkt receive_pkt;
//    int temp_outport;
    int receive_bits;
    unsigned char receive_data, RX_data;
    bool SOF;
    mac temp_mac;
    struct pcap_pkthdr temp_pkthdr;
    sc_time current_time;

    struct pcap_file_header pcaphdr =
    {
        0xa1b2c3d4,  //magic
        0x02,  //version_major
        0x04,  //version_minor
        0,  //thiszone
        0,  //sigfigs
        0xFFFF,  //snaplen, the entire packet is captured
        0x01  //linktype, 1 is Ethernet
    };
    receive_bits = 0;
    receive_data = 0;
    pkt_count = 0;
    outfile = fopen(name(), "wb");

    if(!outfile)
    {
        cout << "open file " << name() << " error!" << endl;
        exit(-1);
    }

    fwrite(&pcaphdr, sizeof(pcap_file_header), 1, outfile);

    while(true)
    {

        if(RXClk.event())
        {
            RX_data = (unsigned char)RX.read();
            receive_data = (receive_data << 1) | RX_data;

            if(receive_bits % 8 == 7)
            {
                receive_pkt.data[receive_bits / 8] = receive_data;
            }
            else if(receive_bits % 8 == 0)
            {

                SOF = RXSOF.read();

                if((bool)SOF && receive_bits > 7)
                {
                    //cout<<"start of frame!"<<endl;
                    receive_pkt.pkt_length = receive_bits / 8;
                    receive_bits = 0;
                    //TODO: record packet to file
                    current_time = sc_time_stamp();
                    temp_mac = src_mac.read();
                    temp_pkthdr.ts.tv_sec = (unsigned int)(current_time.to_double() / 1000000000.0);
                    temp_pkthdr.ts.tv_usec = (unsigned int)(current_time.to_double() - (double)temp_pkthdr.ts.tv_sec * 1000000000.0);
                    //gettimeofday(&(temp_pkthdr.ts), NULL);
                    temp_pkthdr.caplen = receive_pkt.pkt_length;
                    temp_pkthdr.len = receive_pkt.pkt_length;
                    //cout << name() << " caplen: " <<  receive_pkt.pkt_length << endl;
                    fwrite(&temp_pkthdr, sizeof(pcap_pkthdr), 1, outfile);
                    fwrite(receive_pkt.data, receive_pkt.pkt_length, 1, outfile);
                    //cout << name() << " received a packet at " << sc_time_stamp() << endl;
#ifdef OUTPUT_PACKET
                    cout << "                                  .........................." << endl;
                    cout << "                                  New Packet Received" << endl;
                    cout << "                                  SRC Address: " << temp_val.src_mac << endl;
                    cout << "                                  DST Address: " << temp_val.dst_mac << endl;
                    cout << "                                  Packet Value: " << (int)temp_val.data << endl;
                    cout << "                                  .........................." << endl;
#endif
                    pkt_count++;
                }
            }

            receive_bits += 1;
        }

        wait();
    }
}
